package com.itheima.controllerweb;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.POIUtils;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

@RestController
@Slf4j
@RequestMapping("/ordersetting")
public class OrderSettingContriller {

    @Autowired
    private OrderSettingService orderSettingService;// 预约设置服务
    @Autowired
    private ResourceLoader resourceLoader;// 资源加载器

    // 文件下载
    @GetMapping("/download")
    public ResponseEntity<Resource> download(@RequestParam("fileName") String fileName) {

        //加载资源文件
        Resource resource = resourceLoader.getResource("classpath:templates/" + fileName);
        //检查文件是否存在
        if (resource.exists()) {
            //设置响应头信息
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Disposition", "attachment;filename=" + fileName);

            return ResponseEntity.ok()//响应状态码
                    .headers(headers)//响应头信息
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)//二进制流
                    .body(resource);//响应体

        } else {
            //文件不存在  返回404
            return ResponseEntity.notFound().build();
        }
    }


    // 根据月份查询预约设置数据
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){
        try{
            List<OrderSetting> orderSettings = orderSettingService.getOrderSettingByMonth(month);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettings);
        }catch (Exception e){
            log.error(e.toString());
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }

    }

    // 根据日期修改预约设置数据
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByOrderDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            log.error(e.toString());
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }


    // 文件上传
    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile file){
        try{
            List<String[]> list =  POIUtils.readExcel(file);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            List<OrderSetting> orderSettings = new ArrayList<>();
            for (String[] strings : list) {
                orderSettings.add(new OrderSetting(format.parse(strings[0]),Integer.parseInt( strings[1])));
            }
            orderSettingService.upload(orderSettings);
            return new Result(true,MessageConstant.UPLOAD_SUCCESS);

        } catch (Exception e) {
            log.error(e.toString());
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }


}
